<?php
/**
 * 补交款链接
 */
class Application_Model_RepayLink extends Application_Model_DomainObject implements Application_Model_PayInterface{
    
    protected $_id;
    
    protected $_name;
    
    protected $_repayMoney;

    protected $_description;
    
    protected $_link;
    
    protected $_type;
    
    protected $_relationId;
    
    protected $_dateCreated;
    
    protected $_userId;

    protected $_datePaid;
    
    protected $_status;
    
    const STATUS_CREATED=0;
    
    const STATUS_PAID = 1;

    /**
     * 创建一个补交款链接
     * @param type $name 链接名称,如:运单130712QXXEE补交款
     * @param type $description 描述
     * @param type $type 补交款对应的付款项的类型,从Application_Model_PayBill::TYPE_中间取值
     * @param type $relationId 补交款相关的那条记录的ID（运单ID）
     * @param type $money 补交金h额
     * @param type $userId 补交款对应的用户ID
     * @return \Application_Model_RepayLink
     */
    public static function createRepayLink($name,$description,$type,$relationId,$money,$userId){
        $link = new Application_Model_RepayLink(array('name'=>$name,'description'=>$description
                ,'type'=>$type,'relationId'=>$relationId,'repayMoney'=>$money));
        $link->Status = self::STATUS_CREATED;
        $link->DateCreated = date('Y-m-d H:i:s');
        $link->UserId = $userId;
        $link->Link = 'P'. substr(Uuch_Util::getVisitingNumber($userId),1);        
        return $link;
    }
    
    public function setPaid() {
        $this->_status = self::STATUS_PAID;
        $this->_datePaid = date('Y-m-d H:i:s');
//        throw new Exception('uuchtest');
        $payer = Application_Model_PayBill::createPayer($this->_type, $this->_relationId);
        if($payer==null){
            throw new Exception('支付对象不能为空'.$this->_type.'||'.$this->_relationId);
        }
        $payer->setPaid();
        $payer->save();
    }
    
    public function isRepeatPay() {
        if($this->_status == self::STATUS_CREATED){
            return true;
        } 
        return false;
    }
    
    public function getMoney() {
        return $this->_repayMoney;
    }
    
    public function getPayDescription() {
        return $this->_description;
    }
    
    public function getPayName() {
        return $this->_name;
    }
    
    public function getPayCommen() {
        return $this->_name;
    }
    
    public function getBillNo() {
        return $this->_link;
    }
    
    public function canPay($userId) {
        return $this->_status == Application_Model_RepayLink::STATUS_CREATED;
    }
    
    public static function getByRelationId($relationId){
        return self::getDao()->queryOne(array('relationId'=>$relationId));
    }
    
    public static function getByRelationIdType($relationId,$type){
        return self::getDao()->queryOne(array('relationId'=>$relationId,'type'=>$type));
    }
	
	public static function getByRelationIdTypeStatus($relationId, $type, $status){
        return self::getDao()->queryOne(array('relationId'=>$relationId,'type'=>$type, 'status' => $status));
    }
    
    public function getuserinfo(){
        if($this->_userId != null){
            return Application_Model_User::load($this->_userId);
        }
    }
    
    public function getPayLink($type)
    {
        //return "http://old.uuch.com/pay/paying/id/".$this->_id.$type;
		return 'http://www.uuch.com/pay/index/index/type/' . $type . '/id/' . $this->_id;
    }
    
    public static function getMyByFilter($quick,$status,$count,$offset,$starttime,$endtime){        
        $where = array();
        $and = "";
        if($quick != null){
            $where[$and." name like "] = "%".$quick."%";
            $and = " and ";
        }
        if($status != null){
            $where[$and." status = "] = $status;
            $and = " and ";
        }
        if ($starttime != NULL) {
            $where[$and . ' dateCreated >= '] = $starttime;
            $and = ' and ';
        }

        if ($endtime != NULL) {
            $where[$and . ' dateCreated <= '] = $endtime." 23:59:59";
            $and = ' and ';
        }
        return self::getDao()->queryPage($where, "  id desc ", $count, $offset);
    }


    /*
    * 获取总金额
    */
    public static function getSumMoney($relationId, $type)
    {
        $link = new Application_Model_DbTable_RepayLink();
        return $link->getSumMoney($relationId, $type);
    }
}